#include "peripherial_Init.h"
#include "compilCondition.h"

unsigned long baseADCTime;
unsigned long baseProgramCycleTime;
unsigned long base;

void peripherialInit(void)
{

	SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_6MHZ);

	base = SysCtlClockGet();
	baseADCTime = 0x8000;
	baseProgramCycleTime = 300000;		//SysCtlClockGet()/250;

	IntMasterDisable();

   //
    // Enable the peripherals used by the application.
    //
	
#ifdef ADC_ENABLE
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC);
#endif

#ifdef SYS_TIMER0_32B_ENABLE	
	SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
#endif	

#ifdef UART_ENABLE
	SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
#endif

#ifdef PROGRAM_LENGHT_MEASURE
	SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2);
#endif

    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);



//	SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2);	
	
	//
	//Configure UART0 to work at 115200bit per second 8bit lengt 1 stop bit and no pair bit
	//
#ifdef UART_ENABLE
	GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));
#endif

	//
	//Configure ADC3 to measure the potentiometer position
	//triggered by timer 1
	//
#ifdef ADC_ENABLE
    ADCSequenceConfigure(ADC_BASE, 3, ADC_TRIGGER_TIMER, 0);
    ADCSequenceStepConfigure(ADC_BASE, 3, 0, ADC_CTL_CH0 | ADC_CTL_IE | ADC_CTL_END);
    ADCSequenceEnable(ADC_BASE, 3);
    ADCIntEnable(ADC_BASE, 3);
    IntEnable(INT_ADC3);
#endif
	//
	// Configuring Timer 1 as 32 bit periodic timer
	// to triger an ADC3 for measure potenciometer position
	//
#ifdef ADC_TIMER1_16B_B_ENABLE
	TimerConfigure(TIMER1_BASE, TIMER_CFG_16_BIT_PAIR || TIMER_CFG_B_PERIODIC);
    TimerLoadSet(TIMER1_BASE, TIMER_B, baseADCTime);
	TimerControlStall(TIMER1_BASE, TIMER_B, true);
    TimerControlTrigger(TIMER1_BASE, TIMER_B, true);    
	TimerEnable(TIMER1_BASE, TIMER_B);
#endif

#ifdef ADC_TIMER1_32B_ENABLE
	TimerConfigure(TIMER1_BASE, TIMER_CFG_32_BIT_PER);
    TimerLoadSet(TIMER1_BASE, TIMER_A, baseADCTime);
	TimerControlStall(TIMER1_BASE, TIMER_A, true);
    TimerControlTrigger(TIMER1_BASE, TIMER_A, true);    
	TimerEnable(TIMER1_BASE, TIMER_A);
#endif

	//
	// Configure Timer 0 for periodic sincronisation of the main program
	// this is the main cvant time
	//
#ifdef SYS_TIMER0_32B_ENABLE
	TimerConfigure(TIMER0_BASE, TIMER_CFG_32_BIT_PER);
	TimerLoadSet(TIMER0_BASE, TIMER_A, baseProgramCycleTime);
	TimerControlStall(TIMER0_BASE, TIMER_A, true);
	TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    IntEnable(INT_TIMER0A);
	TimerEnable(TIMER0_BASE, TIMER_A);
#endif	
	
#ifdef PROGRAM_LENGHT_MEASURE
	TimerConfigure(TIMER2_BASE, TIMER_CFG_32_BIT_PER);
	TimerLoadSet(TIMER2_BASE, TIMER_A, ~0L);
	TimerControlStall(TIMER2_BASE, TIMER_A, true);
	TimerEnable(TIMER2_BASE, TIMER_A);
#endif
	
	
	//
    // Initialize the OLED display.
    //
	OSRAM96x16x1Init(true);

	IntMasterEnable();
}
